/*
Copyright (c) 2009 www.aery.cn See:
http://code.google.com/p/taobaoas3sdk/
If you have questions, please send an email to 七文<imeiren.com@gmail.com>
*/

/*
//Examples:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="__init__()">
<mx:Script>
	<![CDATA[
		import mx.rpc.events.ResultEvent;
		import mx.formatters.DateFormatter;
		
		private var taobao:as3SDK = new as3SDK();
		private var nowTime:Date=new Date();
		
		private function __init__():void{
						
			taobao.app_key = 'test';
			taobao.app_secret = 'test';
			
			taobao.paramArray = taobao.paramArray_init;
			taobao.paramArray.push({'key':'method', 'value':'taobao.taobaoke.items.get'});
			taobao.paramArray.push({'key':'fields', 'value':'iid,title,nick,pic_url,price,click_url'});
			taobao.paramArray.push({'key':'pid', 'value':'mm_5410_0_0'});
			taobao.paramArray.push({'key':'cid', 'value':'1512'});
			taobao.paramArray.push({'key':'page_no', 'value':'1'});
			taobao.paramArray.push({'key':'page_size', 'value':'6'});
			
			taobao.nowTimeFormatter.formatString = "YYYY-MM-DD JJ:NN:SS";
			taobao.paramArray.push({'key':'timestamp', 'value':taobao.nowTimeFormatter.format(nowTime)});
			
			
			var sign:String=taobao.createSign(taobao.paramArray);	//获得整理加密后的_sign		
			taobao.paramArray.push({'key':'sign', 'value':sign});		//把参加放入对象
			
			taobao.server.addEventListener(ResultEvent.RESULT, getTestData);	//监听成功返回数据
			taobao.useHttpService( taobao.createStrParam(taobao.paramArray) );	//使用httpService发送请求
		}

		public function getTestData(event:ResultEvent):void{
			
			var result:Object = event.result as Object;
			trace(result);
		}
	]]>
</mx:Script>	
</mx:WindowedApplication>
*/
package
{
	import com.adobe.crypto.MD5;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class as3SDK
	{
		//初始化
		public var app_secret:String = 'test';	//app_secret
		public var app_key:String = 'test';	//app_key
		public var data_format:String = 'xml';	//数据格式[xml|json]
		public var server_url:String = 'http://gw.sandbox.taobao.com/router/rest';	//服务器地址
		public var server_version:String = '1.0';	//版本号
		
		public var paramArray_init:Array = [
		{'key':'app_key', 'value':app_key},
		{'key':'format', 'value':data_format},
		{'key':'v', 'value':server_version}
		];//初始化参数
		
		public var paramArray:Array = new Array();
		
		//public var nowTime:Date=new Date();
		public var nowTimeFormatter:DateFormatter = new DateFormatter();
		public var server:HTTPService = new HTTPService();;
		public var result:Object = new Object();
				
		public function as3SDK()
		{
		}
		
		//签名函数
		public function createSign(parameterArray:Array):String{
			paramArray.sortOn('key');
			var sign:String = app_secret
			for each(var item:Object in parameterArray){
				sign+=item.key+item.value;
			}
			
			sign = MD5.hash(sign);
			return sign.toUpperCase();
		}
		
		//组参数函数
		public function createStrParam(parameterArray:Array):Object{
			var args:Object = new Object();
			for each(var item:Object in parameterArray){
				//args[item.key] = JSON.encode(item.value);
				args[item.key] = item.value;
			}
			return args;
		}
		
		//使用httpService通信
		public function useHttpService(args:Object):void{		
			//server = new HTTPService();
			server.url = server_url;
			server.method = "GET";
			//server.addEventListener(ResultEvent.RESULT, ReturnHttpResult);
			server.addEventListener(FaultEvent.FAULT, ReturnHttpFault);
			
			server.send(args);
		}
		
		//httpService成功返回结果
		public function ReturnHttpResult(event:ResultEvent):void{
			result = event.result as Object;
			trace(result);
		}
		
		//httpService失败信息
		public function ReturnHttpFault(event:FaultEvent):void{}
	}
}